#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct ListNode
{
	struct ListNode *perv;
	struct ListNode *next;
	int data;
}SL;
void ListErase(SL *pos);
void SqlistInsert(SL* phead, SL *pos, int x);
SL* BuyListNode(int x)
{
	SL *newnode = (SL*)malloc(sizeof(SL));
	newnode->perv = NULL;
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
SL* SqlistInit()
{
	SL *phead = BuyListNode(0);
	phead->perv = phead;
	phead->next = phead;
	return phead;
}
void ListPushBack(SL *phead, int x)
{
	//SL *tail = phead->perv;
	//SL *newnode = BuyListNode(x);
	//tail->next = newnode;
	//newnode->perv = tail;
	//newnode->next = phead;
	//phead->perv = newnode;
	SqlistInsert(phead, phead, x);
}
void Print(SL *plist)
{
	assert(plist);
	SL *cur = plist->next;
	while (cur != plist)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
void SqlistPushFront(SL *phead,int x)
{
	/*SL *newnode = BuyListNode(x);
	SL *frint = phead->next;
	phead->next = newnode;
	newnode->perv = phead;
	newnode->next = frint;
	frint->perv = newnode; */
	SqlistInsert(phead, phead->next, x);
}
void ListPopFront(SL *phead)
{
	//assert(phead);
	//assert(phead->next != phead);
	//SL *frint = phead->next;
	//SL *second = frint->next;
	//phead->next = second;
	//second->perv = phead;
	//free(frint);
	//frint = NULL;
	ListErase(phead->next);
}
void ListPopBack(SL *phead)
{
	//assert(phead);
	//assert(phead->perv != phead);
	//SL *tail = phead->perv;
	//SL *ptail = tail->perv;
	//ptail->next = phead;
	//phead->perv = ptail;
	//free(tail);
	//tail = NULL;
	ListErase(phead->perv);
}
SL* SqlistFind(SL *phead,int x)
{
	assert(phead);
	SL*cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void SqlistInsert(SL* phead, SL *pos, int x)
{
	SL* newnode = BuyListNode(x);
	SL* cur = phead->next;
	while (cur->next != pos)
	{
		cur = cur->next;
	}
	cur->next = newnode;
	newnode->perv = cur;
	newnode->next = pos;
	pos->perv = newnode;
}
void ListErase(SL *pos)
{
	SL *frint = pos->perv;
	SL *tail = pos->next;
	frint->next = tail;
	tail->perv = frint;
	free(pos);
	pos = NULL;
}
void ListDestory(SL *phead)
{
	SL *cur = phead->next;
	while (cur != phead)
	{
		SL* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
	phead = NULL;
}
void SqlistF(SL *phead)
{
	SL *n1 = NULL;
	SL* n2 = phead;
	SL* n3 = n2->next;
	while (n2) 
	{
		n2->next = n1;
		n1 = n2;
		n2 = n3;
		if (n3)
		{
			n3 = n3->next;
		}
	}
}
SL* Sqlistrever(SL* phead)
{
	SL *newno = NULL;
	SL *cur = phead;
	while (phead)
	{
		if (cur->next == NULL)
		{
			SL*next = cur;
			cur->next = newno;
			newno = cur;
			cur = next;
		}
		else
		{
			SL*next = cur->next;
			cur->next = newno;
			newno = cur;
			cur = next;
		}
	}
	return newno;
}
int main()
{
	SL *plist = SqlistInit();
	ListPushBack(plist, 1);
	ListPushBack(plist, 2);
	ListPushBack(plist, 3);
	ListPushBack(plist, 4);
	//SqlistPushFront(plist, 10);
	//ListPopFront(plist);
	//ListPopBack(plist);
	Print(plist);
	SL *newn = Sqlistrever(plist);
	//Sqlistrever(plist);
	//SqlistF(plist);
	Print(newn);
	system("pause");
	return 0;
}